#pragma once
#include <atlbase.h>
#include <atlwin.h>

class CXMPQtPre : public CWindowImpl<CXMPQtPre>
{
private:
	CXMPQtPre(void){};
	~CXMPQtPre(void);

public:
	static CXMPQtPre* Instance()
	{
		static CXMPQtPre s;
		return &s;
	}
	static ATL::CWndClassInfo& GetWndClassInfo() 
	{ 
		static TCHAR szClassName[64] = {0};
		if('\0' == szClassName[0])
		{
			TCHAR szExePath[_MAX_PATH] = {0};
			GetModuleFileName(NULL, szExePath , _MAX_PATH );
			::PathRemoveExtension(szExePath);
			TCHAR * pszName = ::PathFindFileName(szExePath);
			if(NULL == pszName)
				pszName = _T("XMPQT");
			CComBSTR bstrName = pszName;
			bstrName.ToUpper();

			_snwprintf(szClassName, 64, _T("%s_CMD_HELPER{79F0EF0C-0331-4f14-B5BD-D1B7B3E7E0BF}"), bstrName.m_str);

		}
		static ATL::CWndClassInfo wc = 
		{ 
			{ sizeof(WNDCLASSEX), CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS, StartWindowProc, 
				0, 0, NULL, NULL, NULL, (HBRUSH)(COLOR_WINDOW + 1), NULL, szClassName, NULL }, 
				NULL, NULL, IDC_ARROW, TRUE, 0, _T("") 
		}; 

		return wc; 

	}

	static DWORD GetWndStyle(DWORD dwStyle)
	{
		return dwStyle == 0 ? WS_CHILD: dwStyle;
	}

	BEGIN_MSG_MAP(CXMPQtPre)
		MESSAGE_HANDLER(WM_COPYDATA,		OnCopyData)
		MESSAGE_HANDLER(WM_CREATE,			OnCreate)
	END_MSG_MAP()

	LRESULT OnCopyData(UINT , WPARAM , LPARAM , BOOL& );	
	LRESULT OnCreate(UINT , WPARAM , LPARAM , BOOL& );

	std::vector<CallbackNode> m_allCallBack;
	void Fire_LuaEvent(const char* pszKey, const char* bstrParams)
	{		
		Fire_LuaEvent( pszKey, A2BSTR(bstrParams));
	}
	void Fire_LuaEvent(const char* pszKey, BSTR bstrParams)
	{
		TSAUTO();
		for(size_t i = 0;i<m_allCallBack.size();i++)
		{
			m_allCallBack[i].pCallBack(m_allCallBack[i].userData1,m_allCallBack[i].userData2, pszKey,bstrParams);
		}
	}

	int AttachListener(DWORD userData1,DWORD userData2,funResultCallBack pfn, const void* pfun);
	int DetachListener(DWORD userData1, const void* pfun);

};


